package cn.jboa.service.impl;

import cn.jboa.common.Constants;
import cn.jboa.dao.CheckResultDao;
import cn.jboa.dao.CliamVouCherDao;
import cn.jboa.dao.CliamVouCherDetailDao;
import cn.jboa.entity.BizClaimVoucher;
import cn.jboa.entity.BizClaimVoucherDetail;
import cn.jboa.service.CliamVouCherService;
import cn.jboa.util.PaginationSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("cliamVouCherService")
public class CliamVouCherServiceImpl implements CliamVouCherService {

    @Autowired
    @Qualifier("cliamVouCherDao")
    private CliamVouCherDao cliamVouCherDao;

    @Autowired
    @Qualifier("cliamVouCherDetailDao")
    private CliamVouCherDetailDao cliamVouCherDetailDao;

    @Autowired
    @Qualifier("checkResultDao")
    private CheckResultDao checkResultDao;

    public void saveCliamVouCher(BizClaimVoucher claimVoucher) {
        claimVoucher.setCreateTime(new java.util.Date());
        claimVoucher.setModifyTime(claimVoucher.getCreateTime());
        cliamVouCherDao.save(claimVoucher);
    }

    public PaginationSupport<BizClaimVoucher> getClimVoucherPage(String createSn, String status,
                                                                 Date startDate, Date endDate,
                                                                 Integer pageNo, Integer pageSize) {
        PaginationSupport<BizClaimVoucher> result=new PaginationSupport<BizClaimVoucher>();
        if(pageNo>0){
            result.setCurrPageNo(pageNo);
        }
        if(pageSize>0){
            result.setPageSize(pageSize);
        }
        StringBuilder hqlBuilder=new StringBuilder("from BizClaimVoucher where creator.sn= ?");
        List<Object> values= new ArrayList<Object>();
        values.add(createSn);
        if(status != null && status.length() != 0){
            hqlBuilder.append(" and status = ?");
            values.add(status);
        }
        if(startDate != null ){
            hqlBuilder.append(" and createTime >= ?");
            values.add(startDate);

        }
        if(endDate != null ){
            hqlBuilder.append(" and createTime < ?");
            Calendar oneDayLate= Calendar.getInstance();
            oneDayLate.setTime(endDate);
            oneDayLate.add(Calendar.DAY_OF_MONTH,1);
            values.add(oneDayLate.getTime());

        }
        int count= cliamVouCherDao.getTotalCount(hqlBuilder.toString(),values.toArray()).intValue();
        result.setTotalCount(count);

        if(count > 0){
            int pageCount=(count+result.getPageSize()-1)/result.getPageSize();
            result.setTotalPageCount(pageCount);
            if(result.getCurrPageNo() > pageCount){
                result.setCurrPageNo(pageCount);
            }
            if(status == null || status.length() == 0)
                hqlBuilder.append("order by status,createTime desc");
            List<BizClaimVoucher> items=cliamVouCherDao.findForPage(hqlBuilder.toString(),
                    result.getCurrPageNo(),result.getPageSize(),values.toArray());
            result.setItems(items);
        }
        return result;
    }

    public BizClaimVoucher findClaimVoucherById(int id) {
        return cliamVouCherDao.get(id);
    }

    public void updateClaimVoucher(BizClaimVoucher claimVoucher) {
        BizClaimVoucher toUpdate = cliamVouCherDao.get(claimVoucher.getId());
        for (BizClaimVoucherDetail b :toUpdate.getDetailList()) {
            cliamVouCherDetailDao.delete(b);
        }
        toUpdate.setModifyTime(new Date());
        toUpdate.setTotalAccount(claimVoucher.getTotalAccount());
        toUpdate.setEvent(claimVoucher.getEvent());
        if (claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)) {
            toUpdate.setStatus(claimVoucher.getStatus());
            toUpdate.setNexDeal(claimVoucher.getNexDeal());
        }
        toUpdate.setDetailList(claimVoucher.getDetailList());
        for (BizClaimVoucherDetail d : toUpdate.getDetailList()) {
            d.setClaimVoucher(toUpdate);
        }
    }

    public void deleteClaimVoucher(BizClaimVoucher claimVoucher) {
        //删除相关明细
        cliamVouCherDetailDao.deleteByCliamVouCher(claimVoucher);
        //删除相关审核记录
        checkResultDao.deleteByClaimVoucher(claimVoucher);
        //删除报销单
        cliamVouCherDao.delete(claimVoucher);

    }

    public CliamVouCherDao getCliamVouCherDao() {
        return cliamVouCherDao;
    }

    public void setCliamVouCherDao(CliamVouCherDao cliamVouCherDao) {
        this.cliamVouCherDao = cliamVouCherDao;
    }

    public CliamVouCherDetailDao getCliamVouCherDetailDao() {
        return cliamVouCherDetailDao;
    }

    public void setCliamVouCherDetailDao(CliamVouCherDetailDao cliamVouCherDetailDao) {
        this.cliamVouCherDetailDao = cliamVouCherDetailDao;
    }

    public CheckResultDao getCheckResultDao() {
        return checkResultDao;
    }

    public void setCheckResultDaoImpl(CheckResultDao checkResultDao) {
        this.checkResultDao = checkResultDao;
    }
}
